package com.immunology.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.model.MappingPages;
import com.immunology.model.User;
import com.immunology.service.CreateUserService;
import com.immunology.service.RegistrationPostService;
import com.immunology.service.UserRegistrationValidService;

public class UserRegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getServletContext().getRequestDispatcher(MappingPages.REGISTRATION_JSP)
				.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		User user = null;
		RegistrationPostService registrationPostService = new RegistrationPostService();
		registrationPostService.uploadRegistrationData(request, response);
		Map<String, String> parameters = registrationPostService
				.getParameters();
		UserRegistrationValidService registrationValidService = new UserRegistrationValidService();
		boolean isValidParameter = registrationValidService.isValid(parameters);
		Map<String, String> mapError = registrationValidService.getErrorMap();
		if (isValidParameter) {
			user = new CreateUserService().createUser(parameters);
			request.getRequestDispatcher(MappingPages.ADMIN_JSP)
					.forward(request, response);

		} else {
			response.setCharacterEncoding("UTF-8");
			request.setAttribute("mapError", mapError);
			getServletContext().getRequestDispatcher(
					MappingPages.REGISTRATION_JSP).forward(request, response);
		}

	}
}
